package com.bifang.module.file.core.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.file.common.enums.FileFieldParamDictEnum;
import com.bifang.module.system.core.dao.model.SysUser;
import com.bifang.module.system.core.dao.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;

import java.util.Arrays;
import java.util.List;
import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.module.file.common.vo.fileFieldParam.FileFieldParamVO;
import com.bifang.module.file.common.vo.fileFieldParam.FileFieldParamKeyVO;
import com.bifang.module.file.common.vo.fileFieldParam.FileFieldParamQueryVO;
import com.bifang.module.file.core.biz.FileFieldParamIntegration;
import com.bifang.module.file.dao.service.FileFieldParamService;
import com.bifang.module.file.dao.model.FileFieldParam;
/**
 * 文件元数据字段参数配置表业务实现类
 *
 * @author xyh
 * @since 2023-03-15
 */
@ApiService(funcCode = "fileFieldParam", title = "文件元数据字段参数配置表")
public class FileFieldParamIntegrationImpl implements FileFieldParamIntegration {

    @Autowired
    public FileFieldParamService fileFieldParamService;
    @Autowired
    public SysUserService sysUserService;
    @Autowired
    public DictHelper dictHelper;

    @Override
    @OpApi(funcCode = "fileFieldParam0001", title = "文件元数据字段参数配置表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<FileFieldParamVO> getList(FileFieldParamQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        QueryWrapper<FileFieldParam> fileFieldParamWrapper = new QueryWrapper<>();
        fileFieldParamWrapper
                .lambda()
                .eq(!ObjectUtils.isEmpty(vo.getFieldType()), FileFieldParam::getFieldType, vo.getFieldType())
                .eq(!ObjectUtils.isEmpty(vo.getFieldKey()), FileFieldParam::getFieldKey, vo.getFieldKey())
                .eq(!ObjectUtils.isEmpty(vo.getFieldName()), FileFieldParam::getFieldName, vo.getFieldName())
                .eq(!ObjectUtils.isEmpty(vo.getStatus()), FileFieldParam::getStatus, vo.getStatus());


        List<FileFieldParam> list = fileFieldParamService.list(fileFieldParamWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<FileFieldParam> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<FileFieldParamVO> resultList = Convert.toList(FileFieldParamVO.class, pageInfo.getList());

        for(FileFieldParamVO fileFieldParamVO:resultList){
            SysUser cretteuser = sysUserService.getById(fileFieldParamVO.getCreateName());
            fileFieldParamVO.setCreateFullName(cretteuser.getFullName());
            SysUser updateuser = sysUserService.getById(fileFieldParamVO.getUpdateName());
            fileFieldParamVO.setUpdateFullName(updateuser.getFullName());
        }

        // 转义数据字典值
        dictHelper.valueToName(resultList, Arrays.asList(FileFieldParamDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "fileFieldParam0002", title = "文件元数据字段参数配置表根据主键查询详情", funcType = FuncTypeEnum.query)
    public FileFieldParamVO getById(FileFieldParamKeyVO vo) {
        FileFieldParam entity = fileFieldParamService.getById(vo.getFieldId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        FileFieldParamVO result = Convert.convert(FileFieldParamVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "fileFieldParam0003", title = "文件元数据字段参数配置表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(FileFieldParamVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "fileFieldParam0004", title = "文件元数据字段参数配置表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "fileFieldParam0004",
            operationName = "文件元数据字段参数配置表新增",
            dataType = "fieldId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public FileFieldParamVO add(FileFieldParamVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        FileFieldParam entity = Convert.convert(FileFieldParam.class, vo);
        fileFieldParamService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setFieldId(entity.getFieldId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "fileFieldParam0005", title = "文件元数据字段参数配置表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "fileFieldParam0005",
            operationName = "文件元数据字段参数配置表修改",
            dataType = "fieldId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public FileFieldParamVO edit(FileFieldParamVO vo) {
        if (ObjectUtils.isEmpty(vo.getFieldId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        FileFieldParam oldEntity = fileFieldParamService.getById(vo.getFieldId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        FileFieldParam entity = Convert.convert(FileFieldParam.class, vo);
        fileFieldParamService.updateById(entity);
        fileFieldParamService.clearKeyCache(entity.getFieldKey());
        return vo;
    }

    @Override
    @OpApi(funcCode = "fileFieldParam0006", title = "文件元数据字段参数配置表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "fileFieldParam0006",
            operationName = "文件元数据字段参数配置表删除",
            dataType = "fieldId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(FileFieldParamKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getFieldId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        FileFieldParam entity = fileFieldParamService.getById(vo.getFieldId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        fileFieldParamService.removeById(vo.getFieldId());
        fileFieldParamService.clearKeyCache(entity.getFieldKey());
    }
}
